* This do-file reproduces the results for Table 1 and Figure 1 from article
* Also reproduces results found in Tables A1, A3, and A4 of Supplementary Information

* Open the wide-form version of the 2010-2014 CCES Panel Survey
use cces_panel_2010_2014.dta, clear

* Create demographic control variables for robustness checks
recode gender 1=0 2=1, gen(female)
recode race 2/9=0, gen(nonwhite)
gen age=2014-birthyr
gen lostjobcutbenefits=1 if CC14_387_4==1 | CC14_387_4==1
replace lostjobcutbenefits=0 if CC14_387_4==2 & CC14_387_4==2
recode CC14_302 1=4 2=3 3=2 4=1 5=0 6=., gen( econassess)
recode faminc_14 1=0 2=10 3=20 4=30 5=40 6=50 7=60 8=70 9=80 10=100 11=120 12=150 13=200 14=250 15=350 16=500 32=250 97=., gen(income)

* Activate the svy: option for the sampling weights
svyset [pw=weight]

* Generate variables for how different R sees him/herself from rest of people in the district
gen outofstep=0
replace outofstep=outofstep+1 if CC14_332A~=CC14_DISTRC1_post & CC14_332A~=. & CC14_DISTRC1_post~=.
replace outofstep=outofstep+1 if CC14_332B~=CC14_DISTRC2_post & CC14_332B~=. & CC14_DISTRC2_post~=.
replace outofstep=outofstep+1 if CC14_332E~=CC14_DISTRC3_post & CC14_332E~=. & CC14_DISTRC3_post~=.
replace outofstep=outofstep+1 if CC14_322G~=CC14_DISTRC4_post & CC14_322G~=. & CC14_DISTRC4_post~=.
replace outofstep=outofstep+1 if CC14_322H~=CC14_DISTRC5_post & CC14_322H~=. & CC14_DISTRC5_post~=.

* Generate variable for whether R thinks MC matches/mismatches population of district on race/ethnicity
gen racematch_14=1 if (CC14_306a==2 &  CC14_388a==3) | (CC14_306a==3 &  CC14_388a==1) | (CC14_306e==2 &  CC14_388a==3 & HouseCand1IncumbentNum==1) | (CC14_306f==2 &  CC14_388a==3 & HouseCand2IncumbentNum==1)
gen racemismatch_14=1 if (CC14_306a==2 &  CC14_388a~=3 ) | (CC14_306a==3 &  CC14_388a~=1 ) | (CC14_306e==2 &  CC14_388a~=3 & HouseCand1IncumbentNum==1) | (CC14_306f==2 &  CC14_388a~=3 & HouseCand2IncumbentNum==1)
recode racematch_14-racemismatch_14 (.=0)

* Generate variable for whether R thinks MC matches/mismatches population of district on partisanship
gen partydistmatch_14=1 if CC14_310d==2 & CC14_388b==2
replace partydistmatch_14=1 if CC14_310d==3 & CC14_388b==1
recode partydistmatch_14 .=0

gen partydistmismatch_14=1 if CC14_310d==2 & CC14_388b==1
replace partydistmismatch_14=1 if CC14_310d==3 & CC14_388b==2
recode partydistmismatch_14 .=0

* Generate variable for R's self reported ideology
recode CC14_341A 8=., gen(idself_14)

* Generate variable for R's placement of Incumbent on ideological scale
gen idmember_14=CC14_341K if HouseCand1IncumbentNum==1
replace idmember_14=CC14_341L if HouseCand2IncumbentNum==1
replace idmember_14=CC14_341M if CC14_341M~=.
recode idmember_14 8=.

* Generate variable that is how far R perceives MC to be from ideology of district
recode idmember_14 3/5=3 6=4 7=5, gen(idmember2_14)
gen dist_distance_14=abs(idmember2_14-CC14_DIST1_post)

* Generate variables for whether R had satisfactory contact with MC or recalls pork from MC
recode CC14_364 2=0 .=0, gen(contact_satisfied_14)
recode CC14_365 2=0, gen(pork_recall_14)

* Generate variable for R's Party ID
recode pid7_14 1/3=1 4=0 5/7=-1 8=0, gen(partyid_14)

* Generate a variable for whether R's party is out of step with the congressional district
gen partyoutofstep=1 if (CC14_388b==1 & partyid_14==-1) | (CC14_388b==2 & partyid_14==1) | (CC14_388b<3 & partyid_14==0)
replace partyoutofstep=0 if partyoutofstep==. & CC14_388b~=. & partyid_14~=.

* Genenerate variable for Party of Incumbent
gen inc_14=-1 if CurrentHouseParty=="Republican"
replace inc_14=1 if CurrentHouseParty=="Democratic"
replace inc_14=0 if inc_14==.

* Generate variable for whether R and MC share party
gen sameparty_14=2 if (partyid_14==1 & CC14_310d==3) | (partyid_14==-1 & CC14_310d==2)
replace sameparty_14=0 if (partyid_14==1 & CC14_310d==2) | (partyid_14==-1 & CC14_310d==3)
replace sameparty_14=1 if partyid_14==0 | CC14_310d==5 | CC14_310d==1 | CC14_310d==4

* Generate variable for R's ideological distance from MC
gen distance_14=abs(ideo5-idmember2_14) if ideo5<6

* Generate variable for number of issues on which R and MC agree
gen agreement_14=0
replace agreement_14=agreement_14+1 if CC14_322G==CC14_333_a_1 & CC14_322G~=. & CC14_333_a_1~=. 
replace agreement_14=agreement_14+1 if CC14_333_a_3==CC14_322H & CC14_333_a_3~=. & CC14_322H~=.
replace agreement_14=agreement_14+1 if CC14_333_a_4==CC14_332E & CC14_333_a_4~=. & CC14_332E~=.
replace agreement_14=agreement_14+1 if CC14_333_a_5==CC14_332B & CC14_333_a_5~=. & CC14_332B~=.
replace agreement_14=agreement_14+1 if CC14_333_a_2==CC14_332A & CC14_333_a_2~=. & CC14_332A~=.

* Generate variable for number of issues on which District and MC agree
gen distagreement_14=0
replace distagreement_14=distagreement_14+1 if CC14_DISTRC4_post ==CC14_333_a_1 & CC14_DISTRC4_post ~=. & CC14_333_a_1~=. 
replace distagreement_14=distagreement_14+1 if CC14_333_a_3== CC14_DISTRC5_post & CC14_333_a_3~=. & CC14_DISTRC5_post ~=.
replace distagreement_14=distagreement_14+1 if CC14_333_a_4== CC14_DISTRC3_post & CC14_333_a_4~=. & CC14_DISTRC3_post ~=.
replace distagreement_14=distagreement_14+1 if CC14_333_a_5== CC14_DISTRC2_post & CC14_333_a_5~=. & CC14_DISTRC2_post ~=.
replace distagreement_14=distagreement_14+1 if CC14_333_a_2== CC14_DISTRC1_post & CC14_333_a_2~=. & CC14_DISTRC1_post ~=.

* Rename and recode variables for how well MC represents district and MC approval
rename CC14_367 repdistrict_14
rename CC14_315a approval_14
recode approval_14 1=4 2=3 3=2 4=1 5=.

* Generate an identifier for R's congressional district (to be used for district fixed effects)
gen dist_14=real(cdid)
gen stdist_14=(inputstate*100)+dist_14

* Generate dummies for the categories of repdist
tab repdistrict_14, gen(repdist_14)
ren repdist_141 verywell_14
ren repdist_143 notwell_14
ren repdist_142 somewhatwell_14

* Generate version of DV that excludes not sure and reorders categories
recode repdistrict_14 1=3 3=1 4=., gen(repdistrict2_14)


**** RESULTS

* Produces results shown in Table 1
svy: mlogit repdistrict2_14 dist_distance_14 partydistmatch_14 partydistmismatch_14 distance_14 sameparty_14  
margins, at(dist_distance_14=(0(1)5)) atmeans post
parmest, saving(temp, replace)

* Run following lines of code just to produce predicted effects plot (FIGURE 1)
/*
use temp, clear
gen outcome=1 if _n<7
replace outcome=2 if _n>6 & _n<13
replace outcome=3 if _n>12
gen distance=1 if outcome~=outcome[_n-1]
replace distance=distance[_n-1]+1 if distance==.
replace distance=distance-.1 if outcome==1
replace distance=distance+.1 if outcome==3

twoway rcap min95 max95 distance if outcome==1, lc(gs10) || rcap min95 max95 distance if outcome==2, lc(gs10) || rcap min95 max95 distance if outcome==3, lc(gs10) || connected estimate distance if outcome==1, mc(black) m(C) lp(solid) ||  connected estimate distance if outcome==2, m(S)  mc(black) lp(dash) ||  connected estimate distance if outcome==3, lp(shortdash) scheme(s1mono) m(T) aspect(1) legend(order(4 "Not well" 5 "Somewhat well" 6 "Very well") col(1) pos(3)) mc(black) plotr(lc(none)) xtitle("Perceived distance of incumbent from district") ytitle(" ")
graph export distideology_rep.eps, replace
*/

* Produces results shown in Table A1 of Supplementary Information
svy: mlogit repdistrict2_14 dist_distance_14 partydistmatch_14 partydistmismatch_14 distance_14 sameparty_14  racematch_14 pork_recall_14 contact_satisfied_14 econassess lostjobcutbenefits female nonwhite age educ income

* Produces results shown in Table A4 of Supplementary Information
svy: mlogit repdistrict_14 dist_distance_14 partydistmatch_14 partydistmismatch_14 distance_14 sameparty_14, base(4)


* Produces results shown in Table A3 of Supplementary Information
areg approval_14 repdistrict2_14##partyoutofstep  c.distance_14##partyoutofstep sameparty_14##partyoutofstep if repdistrict2_14<4 [aw=weight], absorb(stdist_14)
margins, over(repdistrict2_14 partyoutofstep) atmeans
marginsplot, xlabel(1 "Not well" 2 "Somewhat well" 3 "Very well") plot2(m(T)) ylabel(1 "Strongly disapprove" 2 3 4 "Strongly approve", angle(0)) xtitle("How well MC Represents Dist") legend(order(3 "Not out of step with partisanship of district" 4 "Out of step with partisanship of district") rows(2) size(vsmall)) ytitle(" ") aspect(1) title(" ")
graph export outofstep_conditional.pdf, replace


